package zhoug.demo.zui.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.security.SecureRandom;
import java.util.Random;

import androidx.appcompat.app.AppCompatActivity;
import zhoug.common.utils.RandomUtils;
import zhoug.demo.zui.R;
import zhoug.logging.Logger;
import zhoug.zui.group.ZUIFlowViewGroup;

public class FlowViewGroupActivity extends AppCompatActivity {

    private static final String TAG = ">>>FlowViewGroupActivity";
    private LinearLayout mLinearLayout;
    private ViewGroup mRoot;
    private ZUIFlowViewGroup mZUIFlowViewGroup;
    private ImageView mBtnAdd;
    private ImageView mBtnReduce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow_view_group);
        findViews();
        bindListener();
    }


    private void findViews() {

        mLinearLayout = findViewById(R.id.linearLayout);
        mRoot = findViewById(R.id.root);
        mZUIFlowViewGroup = findViewById(R.id.ZUIFlowViewGroup);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnReduce = findViewById(R.id.btn_reduce);
    }

    @SuppressLint("ClickableViewAccessibility")
    private void bindListener() {
        //添加child
        mBtnAdd.setOnClickListener(v->{
            TextView child = (TextView) LayoutInflater.from(this).inflate(R.layout.layout_tab_text, mZUIFlowViewGroup, false);
            Random random=new SecureRandom();
            int length;
            int i = random.nextInt(4);
            if(i==2){
                length=random.nextInt(20);
            }else{
                length=random.nextInt(5);
            }
            child.setText((mZUIFlowViewGroup.getChildCount()+1)+ RandomUtils.randomChinese(length));
            mZUIFlowViewGroup.addView(child);
        });
        //减少child
        mBtnReduce.setOnClickListener(v->{
            int childCount = mZUIFlowViewGroup.getChildCount();
            if(childCount>0){
                mZUIFlowViewGroup.removeViewAt(childCount-1);
            }
        });

        mRoot.setOnTouchListener(new View.OnTouchListener() {
            private float lastX = 0;
            private float lastY = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        Logger.d(TAG, "onTouch:ACTION_DOWN");
                        lastX = event.getX();
                        lastY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        Logger.d(TAG, "onTouch:ACTION_UP");
                        lastX=0;
                        lastY=0;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Logger.d(TAG, "onTouch:ACTION_MOVE");
                        float x = event.getX();
                        float y = event.getY();
                        float disX=x-lastX;//小于0 左移
//                        float disY=y-lastY;//小于0 上移
                        ViewGroup.LayoutParams layoutParams = mZUIFlowViewGroup.getLayoutParams();
                        if(layoutParams.width==ViewGroup.LayoutParams.WRAP_CONTENT){
                            layoutParams.width=mZUIFlowViewGroup.getMeasuredWidth();
                        }else{
                            layoutParams.width+=disX;
                        }
//                        layoutParams.height+=disY;
//                        layoutParams.height= ViewGroup.LayoutParams.WRAP_CONTENT;
                        mZUIFlowViewGroup.setLayoutParams(layoutParams);
                        lastX=x;
                        lastY=y;

                        break;
                }

                return true;
            }
        });
    }




}